package response

import (
	"errors"
	"net/http"

	"github.com/gin-gonic/gin"
)

var ErrServer = errors.New("网络异常，请稍后再试")

type Response struct {
	Code int         `json:"code"`
	Msg  string      `json:"msg"`
	Data interface{} `json:"data"`
}

//参数错误response
func ParamError(c *gin.Context, err error) bool {
	if err != nil {
		_ = c.Error(err).SetType(gin.ErrorTypePublic)
		c.JSON(http.StatusOK, Response{
			Code: -1,
			Msg:  "参数错误",
			Data: nil,
		})
		return true
	}
	return false
}

//内部错误response
func ServerError(c *gin.Context, err error) bool {
	if err != nil {
		_ = c.Error(err).SetType(gin.ErrorTypePrivate)
		c.JSON(http.StatusOK, Response{
			Code: -1,
			Msg:  "网络异常，请稍后再试",
			Data: nil,
		})
		return true
	}
	return false
}

//成功响应
func Success(c *gin.Context, msg string, data interface{}) {
	c.JSON(http.StatusOK, Response{
		Code: http.StatusOK,
		Msg:  msg,
		Data: data,
	})
}

//失败响应
func Error(c *gin.Context, msg string) {
	c.JSON(http.StatusOK, Response{
		Code: -1,
		Msg:  msg,
		Data: nil,
	})
}
